//
//  YXMonthView.m
//  跑跑快销
//
//  Created by 杨潇 on 14-5-4.
//  Copyright (c) 2014年 ylife. All rights reserved.
//

#import "YXMonthView.h"

@implementation YXMonthView
@synthesize touch;

- (id)initWithFrame:(CGRect)frame withBlock:(Touch)block
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        touch = block;
        int w = (320 - 80)/3;
        for (int i = 0; i < 2; i ++) {
            UIButton * addSubtract = [UIButton buttonWithType:UIButtonTypeRoundedRect];
            addSubtract.frame = CGRectMake(40 + 2*w*i, 0, w, 30);
            addSubtract.tag = i + 100;
            [addSubtract addTarget:self action:@selector(touchAddSubtract:) forControlEvents:UIControlEventTouchUpInside];
            addSubtract.backgroundColor = [UIColor whiteColor];
            [self addSubview:addSubtract];
        }
        
        NSDateFormatter * yearFormatter = [[NSDateFormatter alloc] init];
        yearFormatter.dateFormat = @"yyyy";
        _showLab = [[UILabel alloc] initWithFrame:CGRectMake(40 + w, 0, w, 30)];
        _showLab.textAlignment = NSTextAlignmentCenter;
        _showLab.text = [yearFormatter stringFromDate:[NSDate date]];
        _showLab.backgroundColor = [UIColor redColor];
        [self addSubview:_showLab];
        
        NSDateFormatter * formatter = [[NSDateFormatter alloc] init];
        formatter.dateFormat = @"MM";
        int month = [[formatter stringFromDate:[NSDate date]] integerValue];

        int y = 0;
        int x = 0;
        int monthBtnW = 320/6;
        for (int i = 0; i < 12; i ++) {
            if (i == 6) {
                y ++;
                x = 0;
            }
            UIButton * monthBtn = [UIButton buttonWithType:UIButtonTypeCustom];
            [monthBtn setBackgroundColor:[UIColor whiteColor]];
            [monthBtn setTitle:[NSString stringWithFormat:@"%d",i + 1] forState:UIControlStateNormal];
            [monthBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
            monthBtn.tag = i + 1;
            [monthBtn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
            monthBtn.frame = CGRectMake(0 + monthBtnW*x,_showLab.frame.origin.y + _showLab.frame.size.height + y*40, monthBtnW, 40);
            monthBtn.layer.borderColor = [UIColor brownColor].CGColor;
            monthBtn.layer.borderWidth = .5;
            [self addSubview:monthBtn];
            if (i + 1 == month) {
                monthBtn.backgroundColor = [UIColor redColor];
                _currentMonth = monthBtn;
            }
            x ++;
        }
    }
    return self;
}

- (void)btnClick:(UIButton *)btn
{
    _currentMonth.backgroundColor = [UIColor whiteColor];
    btn.backgroundColor = [UIColor redColor];
    _currentMonth = btn;
    touch([NSString stringWithFormat:@"%@-%d",_showLab.text,btn.tag]);
}

- (void)touchAddSubtract:(UIButton *)btn
{
    [_currentMonth setBackgroundColor:[UIColor whiteColor]];
    switch (btn.tag) {
        case 100:
        {
            _showLab.text = [NSString stringWithFormat:@"%d",[_showLab.text intValue] - 1];
        }
            break;
        case 101:
        {
            _showLab.text = [NSString stringWithFormat:@"%d",[_showLab.text intValue] + 1];
        }
            break;
            
        default:
            break;
    }
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

@end
